<?php

namespace app\api\controller;
use think\Db;
use app\api\model\Cart as CartModel;

/**
 * 购物车管理
 * Class Cart
 * @package app\api\controller
 */
class Cart extends Controller
{
    /* @var \app\api\model\User $user */
    private $user;

    /* @var \app\api\model\Cart $model */
    private $model;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->user = $this->getUser();
        $this->model = new CartModel($this->user['user_id']);
    }

    /**
     * 购物车列表
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        return $this->renderSuccess($this->model->getList($this->user));
    }

    /**
     * 加入购物车
     * @param $goods_id
     * @param $goods_num
     * @param $goods_sku_id
     * @return array
     */
    public function add($goods_id, $goods_num, $goods_sku_id)
    {
        if (!$this->model->add($goods_id, $goods_num, $goods_sku_id)) {
            return $this->renderError($this->model->getError() ?: '加入购物车失败');
        }
        $total_num = $this->model->getTotalNum();
        return $this->renderSuccess(['cart_total_num' => $total_num], '加入购物车成功');
    }

    /**
     * 减少购物车商品数量
     * @param $goods_id
     * @param $goods_sku_id
     * @return array
     */
    public function sub($goods_id, $goods_sku_id)
    {
        $this->model->sub($goods_id, $goods_sku_id);
        return $this->renderSuccess();
    }

    /**
     * 删除购物车中指定商品
     * @param $goods_sku_id (支持字符串ID集)
     * @return array
     */
    public function delete($goods_sku_id)
    {
        $this->model->delete($goods_sku_id);
        return $this->renderSuccess();
    }



    //获取片区
    public function region($id)  {
        $region=Db::name('category')->where('parent_id',$id)->field('category_id,name')->select()->toArray();

        $regiondata['category_id']=0;
        $regiondata['name']='全部';
        array_unshift($region, $regiondata);
        return $this->renderSuccess(compact('region'));
    }
  
  
       //公寓
    public function house($id)  {
       if(!$id==0){
       $house=Db::name('house')->where('parent_id',$id)->field('id,name')->select()->toArray();
       }else{
       $house=Db::name('house')->field('id,name')->select()->toArray();
       }
      
        

        $regiondata['id']=0;
        $regiondata['name']='全部';
        array_unshift($house, $regiondata);
        return $this->renderSuccess(compact('house'));
    }

    //获取房子
    public function get()  {

        
        $data=request()->param();
        $where['category_id']=$data['address'];
        

        if (!$data['region']==0){
            $where['pianqu']=$data['region'];
        }
        
         if (!$data['type']==0){
             $where['goods_jianjie']=$data['type'];
        }
        
         if (!$data['money']==0){
            $where['jiagequjian']=$data['money'];
        }
      
           if (!$data['house']==0){
                $where['xiaoqu']=$data['house'];
          }
       

      
         $token=request()->param('token');
          if(!empty($token)){
          $where['grade']=['<=',$this->getUser()['grade']];
          }else{
             $where['grade']=['>=',0];
          }
        
 
      
      
        $list=Db::name('goods')->where($where)->where('is_delete',0)->select()->each(function($item,$key){
            $item['type']=$this->gettype($item['goods_jianjie']);
            $item['region']=mb_substr($this->getregion($item['pianqu']),0,1,'utf-8');
            $item['xiaoqu']=mb_substr($this->gethouse($item['xiaoqu']),0,1,'utf-8');
            return $item;
        });




        return $this->renderSuccess(compact('list'));
    }
    //房型
    public function gettype($id){
        $list=Db::name('type')->where('id',$id)->find();
        return $list['name'];
    }

    //片区
    public function getregion($id){
        $list=Db::name('category')->where('category_id',$id)->find();
        return $list['name'];
    }
  
  
      //获取小区
    public function gethouse($id){
        $list=Db::name('house')->find($id);
        return $list['name'];

    }

}
